package com.pan.note.controller;

import com.alibaba.fastjson.JSONObject;
import com.pan.note.enumerate.RedisDataEnum;
import com.pan.note.vo.GameRoomVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 小游戏相关controller
 */
@Controller
@Slf4j
public class GameController {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 加载游戏房间
     * @param model model
     * @return 模版
     */
    @GetMapping("getGameRoom")
    public String getRoom(Model model){
        String roomStr = redisTemplate.opsForHash().entries(RedisDataEnum.GAMEROOM.getMsg()).values().toString();
        List<GameRoomVo> gameRoomVos = JSONObject.parseArray(roomStr, GameRoomVo.class);
        model.addAttribute("gameRooms",gameRoomVos);
        return "common::gameRoom";
    }

    @ResponseBody
    @GetMapping("joinRoom")
    public ResponseEntity joinRoom(Integer roomId, String accountId, String userName){
        GameRoomVo gameRoomVo = new GameRoomVo();
        Map<String,String> userInfo = new HashMap<>();
        userInfo.put(accountId,userName);
        gameRoomVo.setRoomId(roomId);
        gameRoomVo.setUserInfo(userInfo);
        redisTemplate.opsForHash().put(RedisDataEnum.GAMEROOM.getMsg(),roomId,gameRoomVo);
        return ResponseEntity.ok(null);
    }
}
